package com.sgj.callable;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

/**
 * @Author: Guoji Shen
 * @Date: 2021/8/9 13:26
 */
public class Demo1 {

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        // Runnable 接口创建线程
        new Thread(new MyThread1(), "AA").start();

        // Callable 接口创建线程
        FutureTask<Integer> futureTask = new FutureTask<>(new MyThread2());
        // lambda 表达式简化
        FutureTask<Integer> futureTask2 = new FutureTask<>(() -> {
            System.out.println(Thread.currentThread().getName() + " come in callable");
            return 1024;
        });
        new Thread(futureTask2, "lucy").start();
        new Thread(futureTask, "mary").start();

        // while (!futureTask2.isDone()) {
        //     System.out.println("wait......");
        // }
        System.out.println(futureTask2.get());
        System.out.println(futureTask.get());
        System.out.println(Thread.currentThread().getName() + "come over");

    }
}

// 实现 Runnable 接口
class MyThread1 implements Runnable {

    @Override
    public void run() {

    }
}

// 实现 Callable 接口
class MyThread2 implements Callable {

    @Override
    public Integer call() throws Exception {
        System.out.println(Thread.currentThread().getName() + " come in callable");
        return 200;
    }
}